using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;

namespace Blader.UI.TextBox
{
    /// <summary>
    /// Summary description for CoolTextBox.
    /// </summary>
    public class CoolTextBox : UserControl
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private readonly Container components;

        private AutoCompleteTextBox innerTextBox;

        private Color borderColor = Color.LightSteelBlue;

        public CoolTextBox()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.DoubleBuffer, true);
            SetStyle(ControlStyles.ResizeRedraw, true);

            // This call is required by the Windows.Forms Form Designer.
            InitializeComponent();
        }

        public Color BorderColor
        {
            get { return borderColor; }
            set
            {
                if (borderColor == value) return;
                borderColor = value;
                Invalidate();
            }
        }

        public Color SelectedItemBackColor
        {
            get { return innerTextBox.PopupSelectionBackColor; }
            set { innerTextBox.PopupSelectionBackColor = value; }
        }

        public Color SelectedItemForeColor
        {
            get { return innerTextBox.PopupSelectionForeColor; }
            set { innerTextBox.PopupSelectionForeColor = value; }
        }

        [Editor(typeof(AutoCompleteEntryCollection.AutoCompleteEntryCollectionEditor), typeof(UITypeEditor))]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [Browsable(false)]
        public AutoCompleteEntryCollection Items
        {
            get { return innerTextBox.Items; }
            set { innerTextBox.Items = value; }
        }

        [Editor(typeof(AutoCompleteTriggerCollection.AutoCompleteTriggerCollectionEditor), typeof(UITypeEditor))]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [Browsable(false)]
        public AutoCompleteTriggerCollection Triggers
        {
            get { return innerTextBox.Triggers; }
            set { innerTextBox.Triggers = value; }
        }

        [Browsable(true)]
        public override string Text
        {
            get { return innerTextBox.Text; }
            set { innerTextBox.Text = value; }
        }

        public override Color ForeColor
        {
            get { return innerTextBox.ForeColor; }
            set { innerTextBox.ForeColor = value; }
        }

        public override ContextMenu ContextMenu
        {
            get { return innerTextBox.ContextMenu; }
            set { innerTextBox.ContextMenu = value; }
        }

        public int PopupWidth
        {
            get { return innerTextBox.PopupWidth; }
            set { innerTextBox.PopupWidth = value; }
        }

        public bool PopupActived
        {
            get { return innerTextBox.PopupActived; }
        }

        public event AutoCompleteEntrySelectedHanler EntrySelectedEvent;

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Rectangle rect = ClientRectangle;
            rect.Width -= 1;
            rect.Height -= 1;
            var p = new Pen(BorderColor);
            e.Graphics.DrawRectangle(p, rect);

            p = new Pen(Color.FromArgb(100, BorderColor));
            rect.Inflate(-1, -1);
            e.Graphics.DrawRectangle(p, rect);

            p = new Pen(Color.FromArgb(45, BorderColor));
            rect.Inflate(-1, -1);
            e.Graphics.DrawRectangle(p, rect);

            p = new Pen(Color.FromArgb(15, BorderColor));
            rect.Inflate(-1, -1);
            e.Graphics.DrawRectangle(p, rect);
        }

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        private void TextBoxSizeChanged(object sender, EventArgs e)
        {
            Height = (sender as AutoCompleteTextBox).Height + 8;
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.innerTextBox = new Blader.UI.TextBox.AutoCompleteTextBox();
            this.SuspendLayout();
            // 
            // innerTextBox
            // 
            this.innerTextBox.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.innerTextBox.Dock = System.Windows.Forms.DockStyle.Fill;
            this.innerTextBox.Location = new System.Drawing.Point(4, 4);
            this.innerTextBox.Name = "innerTextBox";
            this.innerTextBox.PopupBorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.innerTextBox.PopupOffset = new System.Drawing.Point(12, 4);
            this.innerTextBox.PopupSelectionBackColor = System.Drawing.SystemColors.Highlight;
            this.innerTextBox.PopupSelectionForeColor = System.Drawing.SystemColors.HighlightText;
            this.innerTextBox.PopupWidth = 120;
            this.innerTextBox.Size = new System.Drawing.Size(120, 13);
            this.innerTextBox.TabIndex = 0;
            this.innerTextBox.AutoCompleteEntrySelectedHanlerEvent += new Blader.UI.TextBox.AutoCompleteEntrySelectedHanler(this.InnerTextBox_EntrySelectedEvent);
            // 
            // CoolTextBox
            // 
            this.BackColor = System.Drawing.SystemColors.Window;
            this.Controls.Add(this.innerTextBox);
            this.Name = "CoolTextBox";
            this.Padding = new System.Windows.Forms.Padding(4);
            this.Size = new System.Drawing.Size(128, 22);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        void InnerTextBox_EntrySelectedEvent(object sender, IAutoCompleteEntry entry)
        {
            if (EntrySelectedEvent != null)
            {
                EntrySelectedEvent(this, entry);
            }
        }

        #endregion
    }
}